//
//  MyCollectionVController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/7.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "MyCollectionVController.h"
#import "CollectGoodsVController.h"
#import "CollectJobberVController.h"


@interface MyCollectionVController ()<UIScrollViewDelegate>


{
    UIScrollView *mainScroll;//创建滑动容器
    CollectGoodsVController *oneV;
    CollectJobberVController *twoV;
}


@property (nonatomic, strong)UIButton *rightBtn;

@end

@implementation MyCollectionVController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 1;
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    _goodsBtn.tag = 500;
    _jobberBtn.tag =501;
    
    _goodsBtn.selected = YES;
    self.tempBtn = _goodsBtn;
    [_goodsBtn addTarget:self action:@selector(handleBtnAction2:) forControlEvents:(UIControlEventTouchUpInside)];
    [_jobberBtn addTarget:self action:@selector(handleBtnAction2:) forControlEvents:(UIControlEventTouchUpInside)];
    [self configureNaBar];
    [self configureViewCon];
}

- (void)configureViewCon {
    
    
    //创建一个滑动视图用来滑动Viewcontroller
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 117, kScreen_Width, kScreenHeight-117)];
    //设置代理
    mainScroll.delegate = self;
    mainScroll.scrollEnabled = NO;
    //设置滑动区域
    mainScroll.contentSize = CGSizeMake(kScreen_Width*2, 0) ;
    //打开分页功能
    mainScroll.pagingEnabled = YES;
    //设置背景颜色
    mainScroll.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:mainScroll];
    //设置当前子控制器
    oneV = [[CollectGoodsVController alloc] init];
    [self addChildViewController:oneV];
    oneV.view.frame = CGRectMake(0, 0, kScreen_Width, kScreen_Height);
    //将当前的子视图控制器的view添加到主滑动视图上
    [mainScroll addSubview:oneV.view];
    
    
}

- (void)configureNaBar {
        self.navigationItem.title = @"我的收藏";
        
        //设置导航栏按钮的边距
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame= CGRectMake(0, 0, 60, 30);
        [_rightBtn setImage:[UIImage imageNamed:@"all_fanhui2"] forState:(UIControlStateNormal)];
        [_rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn];
        UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
        /**
         width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
         */
        negativeSpacerleft.width = -20;
        self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
        
}

- (void)handleBackAxtion {
        [self.navigationController popViewControllerAnimated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)handleBtnAction2:(UIButton *)sender {
    if(sender!=self.tempBtn)
    {
        self.tempBtn.selected=NO;self.tempBtn=sender;
    }
    self.tempBtn.selected=YES;
    
    
    mainScroll.contentOffset = CGPointMake(kScreen_Width*(sender.tag - 500), 0);
    if (mainScroll.contentOffset.x == kScreen_Width) {
        twoV = [[CollectJobberVController alloc] init];
        [self addChildViewController:twoV];
        twoV.view.frame = CGRectMake(kScreen_Width, 0, kScreen_Width, kScreen_Height);
        [mainScroll addSubview:twoV.view];
    }
}


@end
